﻿using NUnit.Framework;

namespace System.Lua.Tests
{
    [TestFixture]
    public class LuaWriteLiteralExpressionTest : LuaWriterTest
    {
        [Test(Description = "Writes a double literal expression.")]
        public void WriteDoubleLiteralExpressionTest()
        {
            const double value = 5;

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            Assert.AreEqual("5", result);
        }

        [Test(Description = "Writes a string literal expression.")]
        public void WriteStringLiteralExpressionTest()
        {
            const string value = "Hello Lua!";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            Assert.AreEqual("\"Hello Lua!\"", result);
        }

        [Test(Description = "Writes a boolean literal expression.")]
        public void WriteBooleanLiteralExpressionTest()
        {
            const bool value = true;

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            Assert.AreEqual("true", result);
        }

        [Test(Description = "Writes a time literal expression.")]
        public void WriteDateTimeLiteralExpressionTest()
        {
            var value = new DateTime(1984, 10, 5, 8, 45, 0);

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            Assert.AreEqual("465813900", result);
        }

        [Test(Description = "Writes a null literal expression.")]
        public void WriteNullLiteralExpressionTest()
        {
            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(null));

            Assert.AreEqual("nil", result);
        }

        [Test(Description = "Writes a string literal expression that contains new line.")]
        public void WriteStringLiteralExpressionNewLineTest()
        {
            const string value = @"Yo, this is a tweet.
That has multiple lines in-in-in-it";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            // Assert that the string was written in long bracket format
            Assert.AreEqual("[[" + value + "]]", result);
        }

        [Test(Description = "Writes a string literal expression that contains double quote.")]
        public void WriteStringLiteralExpressionDoubleQuoteTest()
        {
            const string value = "2Pac said \"It ain't easy!\". I agree!!!!";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            // Assert that the string was written in long bracket format
            Assert.AreEqual("[[" + value + "]]", result);
        }

        [Test(Description = "Writes a string literal expression that contains double quote.")]
        public void WriteStringLiteralExpressionBackslashTest()
        {
            const string value = @"Who would put a stupid backslash like this \in their tweet messages?\";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            // Assert that the string was written in long bracket format
            Assert.AreEqual("[[" + value + "]]", result);
        }

        [Test(Description = "Writes a string literal expression that contains double closing brackets.")]
        public void WriteStringLiteralExpressionDoubleClosingBracket()
        {
            const string value = "]] } } print(\"Lua code injection!!\")";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            // Assert that the containing long bracket was correctly escaped
            Assert.AreEqual("[[ } } print(\"Lua code injection!!\")]]", result);
        }

        [Test(Description = "Writes a string literal expression that ends with a closing bracket.")]
        public void WriteStringLiteralExpressionEndsWithClosingBracket()
        {
            const string value = "Hello \"new\" [world]";

            string result = GetLuaWriterResult(luaWriter => luaWriter.WriteLiteralExpression(value));

            // Assert that the ending closing bracket was separated by a space from the double closing bracket
            Assert.AreEqual("[[Hello \"new\" [world] ]]", result);
        }
    }
}
